<?php
require './include/global.php';
require './include/validate.php';
require './include/all_left.php';

//folder = 1 :  INBOX

$etypeOptions = array("0"=>"Message","1"=>"Recruiter","2"=>"Candidate","3"=>"Invites");

if($_SESSION[usertype]!="recruiter"){
	echo "<meta http-equiv=\"refresh\" content=\"0;URL=index.php\">";
	exit;
}

//_GET
$extraSQL = '';
$otherParamas = '';
$op_folder = (isset($_GET['op_folder']))?trim($_GET['op_folder']):'0';
$op_fromname = (isset($_GET['op_fromname']))?trim($_GET['op_fromname']):'';
$op_type = (isset($_GET['op_type']))?trim($_GET['op_type']):'';
$op_state = (isset($_GET['op_state']))?trim($_GET['op_state']):'';
$op_keywords = (isset($_GET['op_keywords']))?trim($_GET['op_keywords']):'';
$op_date = (isset($_GET['op_date']))?trim($_GET['op_date']):'';

$smarty->assign("op_folder",$op_folder);
if($op_folder == 1){//Inbox
	$extraSQL .= " AND e_receivedrcid ='".$_SESSION['uid']."' ";
}else{
	$extraSQL .= " AND e_sentrcid ='".$_SESSION['uid']."' ";
}
$smarty->assign("op_folder",$op_folder);
$otherParams .= "&op_folder=$op_folder";

if($op_keywords != ''){
	$extraSQL .= " AND (e_subject like '%".$op_keywords."%' OR e_content like '%".$op_keywords."%') ";
	$smarty->assign("op_keywords",$__GET['op_keywords']);
	$otherParams .= "&op_keywords=$op_keywords";
}
if($op_fromname != ''){
	if($op_folder == 1){
		$extraSQL .= " AND e_fromname like '%".$op_fromname."%' ";
	}else{
		$extraSQL .= " AND e_toname like '%".$op_fromname."%' ";
	}
	$smarty->assign("op_fromname",$__GET['op_fromname']);
	$otherParams .= "&op_fromname=$op_fromname";
}
if($op_date != ''){
	$extraSQL .= " AND ( FROM_UNIXTIME(`e_created`,'%m/%d/%Y') = '".$op_date."') ";
	$smarty->assign("op_date",$op_date);
	$otherParams .= "&op_date=$op_date";
}
if($op_type != ''){
	$extraSQL .= " AND e_type='".$op_type."' ";
	$smarty->assign("op_type",$op_type);
	$otherParams .= "&op_type=$op_type";
}
if($op_state != '') {
	$extraSQL .= " AND e_isread='".$op_state."' ";
	$smarty->assign("op_state",$op_state);
	$otherParams .= "&op_state=$op_state";
}
$smarty->assign("otherParams",$otherParams);

//del
if($_GET[action]=='del'){
	$op_folder = ($_GET['op_folder'] == 1)?"1":"0";
	if(!empty($_POST[delId])){
	  foreach($_POST[delId] as $value){
	     $str[] = "`e_id` = '".$value."' ";
	  }
	  $where = @implode(' OR ',$str);
	  $sql = "DELETE FROM pa_email WHERE ($where)  AND ( e_sentrcid = '".$_SESSION[uid]."' OR e_receivedrcid = '".$_SESSION[uid]."') AND e_type = 0 ";
	  $DB->query($sql);
	  if($op_folder == 1){//INBOX
			$sql = "UPDATE pa_email SET e_receiverdel = 1 WHERE ($where) AND ( e_sentrcid = '".$_SESSION[uid]."' OR e_receivedrcid = '".$_SESSION[uid]."')  AND e_type != 0";
			$DB->query($sql);
	  }else{
			$sql = "UPDATE pa_email SET e_senderdel = 1 WHERE ($where) AND ( e_sentrcid = '".$_SESSION[uid]."' OR e_receivedrcid = '".$_SESSION[uid]."') AND e_type != 0";
			$DB->query($sql);
	  }
	}
	 echo "<meta http-equiv=\"refresh\" content=\"0;URL=myemails.php?op_folder=$op_folder\">";
    exit;
}

$smarty->display('header.tpl');
//echo $extraSQL;

if( $op_folder == 1 ){//INBOX
	$extraSQL .= " AND e_receiverdel= 0 ";
}else{
	$extraSQL .= " AND e_senderdel = 0 ";
}

$where = "  WHERE 1=1 $extraSQL";

$by    = " e_id "; 
$order = " DESC" ;
if($_GET[tp]==1){
	$_GET[order1]==''&&$order1=1;$_GET[order2]==''&&$order2=1;$_GET[order3]==''&&$order3=1;$_GET[order4]==''&&$order4=1;
	switch($_GET[swh]){
	   case 1:
		   if($op_folder == 1){//Inbox
				$by = "e_fromemail";
		   }else{
				$by = "e_toemail";
		   }
		   if($_GET[order1]==1){$order_arr[1]=2; $order=" DESC ";$image_arr[1] = "<img src=\"images/dot1.gif\" align=\"absmiddle\">";}else{$order_arr[1]=1;$order=" ASC ";$image_arr[1] = "<img src=\"images/dot2.gif\"  align=\"absmiddle\">";}
		   break;
	   case 2:
		   $by = "e_subject";
		   if($_GET[order2]==1){$order_arr[2]=2; $order=" DESC ";$image_arr[2] = "<img src=\"images/dot1.gif\"  align=\"absmiddle\">";}else{$order_arr[2]=1;$order=" ASC ";$image_arr[2] = "<img src=\"images/dot2.gif\"  align=\"absmiddle\">";}
		   break;
	   case 3:
			$by = "e_type";
		   if($_GET[order3]==1){$order_arr[3]=2; $order=" DESC ";$image_arr[3] = "<img src=\"images/dot1.gif\"  align=\"absmiddle\">";}else{$order_arr[3]=1;$order=" ASC ";$image_arr[3] = "<img src=\"images/dot2.gif\"  align=\"absmiddle\">";}
		   break;
	   case 4:
		   $by = "e_created";
		   if($_GET[order4]==1){$order_arr[4]=2; $order=" DESC ";$image_arr[4] = "<img src=\"images/dot1.gif\"  align=\"absmiddle\">";}else{$order_arr[4]=1;$order=" ASC ";$image_arr[4] = "<img src=\"images/dot2.gif\"  align=\"absmiddle\">";}
		   break;
	}
		$smarty->assign("order_arr",$order_arr);
		$smarty->assign("image_arr",$image_arr);

}
$order = " ORDER BY $by $order";

$nav = new buildNav;
$nav->other = $otherParams;
$nav->offset = "offset1";
$nav->limit=25;
$result = $DB->fetch_one_array("SELECT COUNT(*) AS count FROM  pa_email $where $order");
$count = $result['count'];

//echo "SELECT COUNT(*) AS count,a.*,b.* FROM pa_applicant as b RIGHT JOIN pa_job as a ON a.jo_id = b.app_jobid $where $order";
$nav->total_result = $count;
//echo "SELECT * FROM pa_email $where $order";
$nav->execute("SELECT * FROM pa_email $where $order");

$i=0;
while($list2 = $DB->fetch_array($nav->sql_result)){
   $datajob[$i]           = $list2;
   $datajob[$i]['e_type'] = $etypeOptions[$list2['e_type']];
   $datajob[$i]['e_created'] = date("m/d/Y",$list2['e_created']);
   $datajob[$i]['detailLink'] = 'sendemail.php?action=view&id='.$list2['e_id'];
   if($list2['e_applicantId']>0){
	 $sql = "SELECT app_isreplied FROM pa_applicant WHERE app_id = '".$list2['e_applicantId']."' ";
	 $appInfo = $DB->fetch_one_array($sql);
	 $datajob[$i]['e_appIsApplied'] = $appInfo['app_isreplied'];
  	 //$datajob[$i]['e_fromname'] = '<a href="applicantdetails.php?id='.$list2['e_applicantId'].'&tpp=posted">'.$list2['e_fromname'].'</a>';
	 if($op_folder == 1){
	 $datajob[$i]['detailLink'] = 'applicantdetails.php?id='.$list2['e_applicantId'].'&tpp=posted&emailId='.$list2['e_id'];
	 }else{
	 $datajob[$i]['detailLink'] = 'applicantdetails.php?recruiter=1&id='.$list2['e_applicantId'];
	 }
   }
   $i++;
}
$smarty->assign("messageList",$datajob);
$page = $nav->pagenav();
$smarty->assign("page",$page);


$smarty->display('allleft.tpl');
$smarty->display('myemails.tpl');
$smarty->display('footer.tpl');
?>